<?php
/**
 * 行情中心
 * @author yu
 */
namespace quote\service;

use common\service\CommonService;
use herosphp\bean\Beans;
use herosphp\core\Loader;

class QuoteService extends CommonService
{
    public function __construct()
    {
        parent::__construct('quoteData');
    }

    /**
     * 获取最新的行情中心数据
     * @param $product_name
     * @param $brand_name
     */
    public function getLastQuote($productName)
    {

        $quoteAnalysisModel = Loader::model('quoteAnalysis');

        $item = $quoteAnalysisModel->getItem(array('product_name'=>$productName), 'adddate', array('adddate' => -1));//找到最新日期

        return $this->getQuote($productName, $item['adddate']);

    }

    /**
     * 获取今天的行情中心数据
     * @param $product_name
     * @param $brand_name
     * @return array|bool|false|mixed
     */
    public function getTodayQuote($productName)
    {
        return $this->getQuote($productName, myDate());
    }

    /**
     * 根据品名、牌号、日期获取行情中心数据
     * @param $product_name
     * @param $date
     * @return array|bool|false|mixed
     */
    public function getQuote($productName, $date)
    {

        $quoteDataService = Beans::get('quote.data.service');

        $quoteAnalysisService = Beans::get('quote.analysis.service');


        $analysisConditions= array('product_name' => $productName,'adddate'=>$date);

        $dataConditions= array('product_name' => $productName,'adddate'=>$date);


        $analysisItem = $quoteAnalysisService->getItem($analysisConditions);

        $dataItem = $quoteDataService->getItem($dataConditions);

        if ($analysisItem && $dataItem) {

            return $analysisItem;

        } else {

            return false;
        }

    }
    
    /**
     * 获取最新的行情中心数据
     * @param $product_name
     * @param $brand_name
     */
    public function getLastQuoteByBrand($brandName)
    {
        $quoteDataModel = Loader::model('quoteData');

        $item = $quoteDataModel->getItem(array('brand_name'=>$brandName), 'adddate', array('adddate' => -1));//找到最新日期

        return $this->getQuoteByBrand($brandName, $item['adddate']);

    }


    /**
     * 获取今天的行情中心数据
     * @param $brand_name
     * @return array|bool|false|mixed
     */
    public function getTodayQuoteByBrand($brandName)
    {
        return $this->getQuoteByBrand($brandName, myDate());
    }

    /**
     * 根据品名、牌号、日期获取行情中心数据
     * @param $brand_name
     * @param $date
     * @return array|bool|false|mixed
     */
    public function getQuoteByBrand($brandName, $date)
    {

        $quoteDataService = Beans::get('quote.data.service');

        $quoteAnalysisService = Beans::get('quote.analysis.service');

        $productName=$this->getProductNameByBrandName($brandName);

        $analysisConditions= array('product_name' => $productName,'adddate'=>$date);

        $dataConditions= array('brand_name' => $brandName,'adddate'=>$date);


        $analysisItem = $quoteAnalysisService->getItem($analysisConditions);

        $dataItem = $quoteDataService->getItem($dataConditions);

        if ($analysisItem && $dataItem) {

            return $analysisItem;

        } else {

            return false;
        }

    }
    /**
     * 获取行情数据中心里的标题
     * @param $analysisItem
     */
    public function getQuoteTitle($adddate, $product_name)
    {
        //2017/03/22 PVC出厂价数据和分析报告
        return date('Y/m/d', strtotime($adddate)) . ' ' . $product_name . '出厂价数据和分析报告';
    }

    public function getProductQuoteTitle($adddate, $product_name)
    {
        //2017年3月28日 现货PVC报价解读
        return date('Y/m/d', strtotime($adddate)) . ' 现货' . $product_name . '报价解读';
    }

    public function getBrandQuoteTitle($adddate, $product_name, $brand_name)
    {
        //2017年3月23日 现货PVC 750A报价解读
        return date('Y/m/d', strtotime($adddate)) . ' 现货' . $product_name . ' ' . $brand_name . '报价解读';
    }

    /**
     * 根据品名获取行情信息，又来显示前台品名行情页面
     * @param $product_name
     * @param $date
     */
    public function getProduct($product_name, $date)
    {


        $quoteAnalysisService = Beans::get('quote.analysis.service');

        $quoteDataService = Beans::get('quote.data.service');

        $conditions['adddate'] = $date;
        $conditions['product_name'] = $product_name;

        $analysisItem = $quoteAnalysisService->getItem($conditions);
        $result['id'] = $analysisItem['id'];
        $result['title'] = $this->getProductQuoteTitle($analysisItem['adddate'], $analysisItem['product_name']);
        $result['adddate'] = $analysisItem['adddate'];
        $result['addtime'] = $analysisItem['addtime'];

        $result['views'] = $analysisItem['views'];
        $result['content'] = $analysisItem['content'];

        $dataItems = $quoteDataService->getItems($conditions);

        $brands = array();

        foreach ($dataItems as $item) {

            $yesterdayBrand = $quoteDataService->getItem(array('brand_name' => $item['brand_name'], 'adddate' => date("Y-m-d", (strtotime($date) - 3600 * 24))));

            if ($yesterdayBrand['price']) {

                $float_price = $item['price'] - $yesterdayBrand['price'];

            } else {

                $float_price = '-';
            }


            $brands[] = array(
                'brand_name' => $item['brand_name'],
                'factory_name' => $item['factory_name'],
                'today_price' => $item['price'],
                'yesterday_price' => $yesterdayBrand['price'],
                'float_price' => $float_price
            );
        }
        $result['brands'] = $brands;
        return $result;


    }


    /**
     * 根据牌号获取行情信息，又来显示前台牌号行情页面
     * @param $brand_name
     * @param $date
     * @param $is_with_product_analysis 是否附带品名分析
     */
    public function getBrand($brand_name, $date, $is_with_product_analysis = false)
    {
        $quoteAnalysisService = Beans::get('quote.analysis.service');
        $quoteDataService = Beans::get('quote.data.service');


        $product_name = $this->getProductNameByBrandName($brand_name);
        $conditions['adddate'] = $date;
        $conditions['brand_name'] = $brand_name;

        $dataItems = $quoteDataService->getItem($conditions);

        $result['id'] = $dataItems['id'];
        $result['title'] = $this->getBrandQuoteTitle($date, $product_name, $brand_name);
        $result['adddate'] = $dataItems['adddate'];
        $result['addtime'] = $dataItems['addtime'];

        $result['views'] = $dataItems['views'];
        $result['brand_name'] = $dataItems['brand_name'];
        $result['factory_name'] = $dataItems['factory_name'];
        $result['today_price'] = $dataItems['price'];
        $result['product_name'] = $product_name;

        $yesterdayBrand = $quoteDataService->getItem(array('brand_name' => $brand_name, 'adddate' => date("Y-m-d", (strtotime($date) - 3600 * 24))));

        if ($yesterdayBrand['price']) {

            $float_price = $dataItems['price'] - $yesterdayBrand['price'];

        } else {

            $float_price = '-';
        }

        $result['yesterday_price'] = $yesterdayBrand['price'];
        $result['float_price'] = $float_price;

        if ($is_with_product_analysis) {

            $analysisItem = $quoteAnalysisService->getItem(array('product_name' => $product_name, 'adddate' => $date));

            $result['content'] = $analysisItem['content'];
        }


        return $result;

    }

    /**
     * 根据牌号找品名
     * @param $brandName
     */
    public function getProductNameByBrandName($brandName)
    {

        $quoteDataService = Beans::get('quote.data.service');

        $dataItems = $quoteDataService->getItem(array('brand_name' => $brandName));

        return $dataItems ? $dataItems['product_name'] : false;

    }
}